'use strict';

/**
 * header service
 */
angular.module('sfhtHaitaoCmsApp')
	.service('HeaderServiceConfig', function($q,
		Gateway,
		sf_b2c_mall_api_cms_getHeaderConfig,
		sf_b2c_mall_api_cms_updateHeaderConfig,
		sf_b2c_mall_api_cms_createHeaderConfig
		) {
		var HeaderServiceConfig = function(options, data) {
			this.options = options;
			this.init(data);
		};
		HeaderServiceConfig.prototype = {
			/**
			 * @override
			 * @function sf.b2c.mall.framework.comm.init
			 * @description 初始化方法
			 * @param  {Object} data 传入的涌来提交服务端的数据
			 */
			init: function(data) {
				this.setData(data);
			},

			/**
			 * @function sf.b2c.mall.framework.setData
			 * @description 修改请求参数
			 */
			setData: function(data) {
				this.data = data;
			},

			/**
			 * 查询顶通配置列表
			 */
			getHeaderConfig: function(topType, pagedQueryOption) {
				var deferred = $q.defer();
				var gateway = new Gateway(sf_b2c_mall_api_cms_getHeaderConfig, {
					topType: topType,
					pagedQueryOption: JSON.stringify(pagedQueryOption)
				});
				gateway.sendRequest(false, true)
					.then(function(data) {
						deferred.resolve(data);
					}, function(data) {
						deferred.reject(data);
					});
				return deferred.promise;
			},
			/**
			 * 创建顶通
			 * @param  {[type]} page [description]
			 * @return {[type]}      [description]
			 */
			createHeaderConfig: function(page) {
				var deferred = $q.defer();
				var gateway = new Gateway(sf_b2c_mall_api_cms_createHeaderConfig, {
					cmsHeaderConfig: JSON.stringify(page)
				});
				gateway.sendRequest(false, true)
					.then(function(data) {
						deferred.resolve(data);
					}, function(data) {
						deferred.reject(data);
					});
				return deferred.promise;
			},
			/**
			 * 修改顶通
			 * @param  {[type]} page [description]
			 * @return {[type]}      [description]
			 */
			updateHeaderConfig: function(page) {
				var deferred = $q.defer();
				var gateway = new Gateway(sf_b2c_mall_api_cms_updateHeaderConfig, {
					cmsHeaderConfig: JSON.stringify(page)
				});
				gateway.sendRequest(false, true)
					.then(function(data) {
						deferred.resolve(data);
					}, function(data) {
						deferred.reject(data);
					});
				return deferred.promise;
			}
		}
		var instance = null
		HeaderServiceConfig.getInstance = function() {
			if (instance) {
				return instance
			} else {
				instance = new HeaderServiceConfig();
				return instance;
			}
		}
		return HeaderServiceConfig;
	});